package cn.chenshiwu121.service.impl;

import cn.chenshiwu121.mapper.ItemCatMapper;
import cn.chenshiwu121.pojo.EasyUITreeNode;
import cn.chenshiwu121.pojo.ItemCat;
import cn.chenshiwu121.pojo.ItemCatExample;
import cn.chenshiwu121.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper mapper;

    @Override
    public List<EasyUITreeNode> getItemCatList(Long parentId) {
        List<EasyUITreeNode> result = new ArrayList<>();
        ItemCatExample example = new ItemCatExample();
        ItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<ItemCat> lists = mapper.selectByExample(example);
        for (ItemCat ic: lists) {
            EasyUITreeNode node = new EasyUITreeNode();
            node.setId(ic.getId());
            node.setState(ic.getIsParent() ? "closed": "open");
            node.setText(ic.getName());
            result.add(node);
        }
        return result;
    }
}
